package com.controller;


import com.config.Result;
import com.pojo.Building;
import com.pojo.VisitorLogs;
import com.pojo.DeleteRecord;
import com.service.BuildService;
import com.service.impl.VisitorLogServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/inout")
public class VisitorLogsController {

    @Autowired
    VisitorLogServiceImpl visitorLogService;

    @Autowired
    BuildService buildService;

    @GetMapping("/search")
    public Result<?> search(@RequestParam String param){
        List<VisitorLogs> visitorLogs = visitorLogService.searchAll(param);

        if (visitorLogs == null){
            return Result.error("-1","查看失败");
        }
        return Result.success(visitorLogs);
    }

    @RequestMapping("/delete")
    public Result<?> delete(@RequestBody DeleteRecord deleteRecord){

        String name = deleteRecord.getName();
        String startTime = deleteRecord.getStartTime();
        String buildName = deleteRecord.getBuildName();
        if (name == null || startTime == null || buildName == null){
            return Result.error("-1","姓名或时间不正确");
        }

        Building building = buildService.selectByName(buildName);
        if (building == null){
            return Result.error("-1","没有该宿舍楼");
        }

        Integer integer = visitorLogService.deleteByInfo(name, startTime, Integer.parseInt(building.getId()));
        if (integer != 1){
            return Result.error("-1","删除失败");
        }
        return Result.success();

    }

    @PostMapping("/add")
    public Result<?> add(@RequestBody VisitorLogs visitorLogs){
        if (visitorLogs == null){
            return Result.error("-1","参数为空");
        }


        Building building = buildService.selectByName(visitorLogs.getBuildName());

        if (building == null){
            return Result.error("-1","没有该宿舍楼");
        }

        Integer integer = visitorLogService.addRecord(visitorLogs, Integer.parseInt(building.getId()));
        if (integer != 1){
            return Result.error("-1","添加失败");
        }

        return Result.success();
    }

}
